﻿Imports ArfaUI
Imports System.Windows.Forms

Public Class TrxPembelianPage
    Inherits PageTemplate
    Private Sub getGudang()
        Dim dt As New DataTable
        If Utils.GetFunction("getGudang", New Object() {"bl"}, dt) Then
            If dt IsNot Nothing Then
                If dt.Rows.Count > 0 Then
                    lblGudang.Text = "Lokasi Penyimpanan : " & dt(0)(0)
                End If
            End If
        End If
    End Sub

    Private Sub GridTransaksi_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles GridTransaksi.EditingControlShowing
        If GridTransaksi.CurrentCell.ColumnIndex = 3 Then
            Dim TextBox1 As TextBox
            TextBox1 = CType(e.Control, TextBox)
            RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            ''ElseIf GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            ' ''    Dim TextBox1 As TextBox
            ''    TextBox1 = CType(e.Control, TextBox)
            ''    RemoveHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox_KeyPress)
            ''    AddHandler TextBox1.KeyPress, New KeyPressEventHandler(AddressOf TextBox1_KeyPress)
        End If
    End Sub
    Private Sub TextBox_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        e.Handled = Utils.HanyaAngka(Asc(e.KeyChar))
    End Sub
    Private Sub GridTransaksi_CellEndEdit(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridTransaksi.CellEndEdit
        If GridTransaksi.CurrentCell.ColumnIndex = 0 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If Not GridTransaksi.CurrentCell.Value = "" Then
                    'blm kelar=? masukkan search equal
                    Dim dt As New DataTable
                    dt = GetAllEqual("vitem", "iditem", GridTransaksi.CurrentCell.Value)
                    If Utils.RowExist(dt) Then
                        GridTransaksi(1, e.RowIndex).Value = IIf(IsDBNull(dt(0)("namaitem")), "", dt(0)("namaitem"))
                        GridTransaksi(2, e.RowIndex).Value = IIf(IsDBNull(dt(0)("hargabeli")), "", dt(0)("hargabeli"))
                        GridTransaksi(4, e.RowIndex).Value = IIf(IsDBNull(dt(0)("satuanbeli")), "", dt(0)("satuanbeli"))
                        GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value
                        sumtotal()
                        sumhutang()
                    Else
                        MsgBox("data tidak terdaftar")
                        GridTransaksi.Rows.RemoveAt(e.RowIndex)
                        GridTransaksi.CurrentCell = GridTransaksi.Item(0, e.RowIndex)
                        GridTransaksi.Item(0, e.RowIndex).Selected = True
                    End If
                End If
            End If
            For Each dgr As DataGridViewRow In GridTransaksi.Rows
                If dgr.Index <> GridTransaksi.CurrentCell.RowIndex Then
                    If dgr.Cells(0).Value = GridTransaksi.CurrentCell.Value Then
                        dgr.Cells(3).Value = dgr.Cells(3).Value + 1
                        dgr.Cells(5).Value = dgr.Cells(3).Value * dgr.Cells(2).Value

                        GridTransaksi.Rows.Remove(GridTransaksi.CurrentRow)
                        sumtotal()
                        sumhutang()
                    End If
                End If

            Next
        ElseIf GridTransaksi.CurrentCell.ColumnIndex = 3 Then
            If Not IsDBNull(GridTransaksi.CurrentCell) Then
                If Not GridTransaksi.CurrentCell.Value = "" Then
                    GridTransaksi(5, e.RowIndex).Value = GridTransaksi(2, e.RowIndex).Value * GridTransaksi(3, e.RowIndex).Value
                    sumtotal()
                    sumhutang()
                End If
            End If
        End If
    End Sub
    Private Sub setinkeypress(ByVal colpos As Integer, ByVal rowpos As Integer, ByVal grid As DataGridView)
        If colpos = grid.Columns("iditem").Index Then
            If rowpos = 0 Then
                grid.CurrentCell = grid.Item("qty", rowpos)
            Else
                grid.CurrentCell = grid.Item("qty", rowpos - 1)
            End If

        ElseIf colpos = grid.Columns("qty").Index Then
            grid.CurrentCell = grid.Item("iditem", rowpos)
            '    If rowpos = 0 Then
            '        grid.CurrentCell = grid.Item("coldiscp", rowpos)
            '    Else
            '        grid.CurrentCell = grid.Item("coldiscp", rowpos - 1)
            '    End If
            'ElseIf colpos = grid.Columns("coldiscp").Index Then
            '    ' grid.CurrentCell = grid.Item("colkodebarang", rowpos)
            '    If rowpos = 0 Then
            '        If grid("coldiscp", rowpos).Value IsNot Nothing Then
            '            If grid("coldiscp", rowpos).Value = 0 Then
            '                grid.CurrentCell = grid.Item("coldiscrp", rowpos)
            '            ElseIf grid("coldiscp", rowpos).Value > 0 Then
            '                grid.CurrentCell = grid.Item("colkodebarang", rowpos)
            '            End If
            '        End If
            '    Else
            '        If grid("coldiscp", rowpos - 1).Value IsNot Nothing Then
            '            If grid("coldiscp", rowpos).Value = 0 Then
            '                grid.CurrentCell = grid.Item("coldiscrp", rowpos - 1)
            '            ElseIf grid("coldiscp", rowpos).Value > 0 Then
            '                grid.CurrentCell = grid.Item("colkodebarang", rowpos)
            '            End If
            '        End If
            '    End If

            '    '----------------
            'ElseIf colpos = grid.Columns("coldiscrp").Index Then
            '    grid.CurrentCell = grid.Item("colkodebarang", rowpos)
        End If
        GridTransaksi.CurrentCell = grid.CurrentCell
        'GridTransaksi.CurrentRow.Selected = True
        GridTransaksi(grid.CurrentCell.ColumnIndex, grid.CurrentCell.RowIndex).Selected = True


    End Sub
    Private Sub GridTransaksi_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Dim rowpos, colpos As Integer
        colpos = GridTransaksi.CurrentCell.ColumnIndex
        rowpos = GridTransaksi.CurrentCell.RowIndex
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Tab) Then
            setinkeypress(colpos, rowpos, GridTransaksi)
        End If
    End Sub

    Private Function GetAllEqual(ByVal tblname As String, ByVal finder As String, ByVal finderkey As String) As DataTable
        Dim dt As New DataTable
        Dim params As Object()
        Params = New Object() {"*", tblname, finder, finderkey}
        If Utils.executeSP("proc_searchallEqual", Params, dt) Then

        End If
        Return dt
    End Function
    Private Function validasirowgrid(ByVal dgr As DataGridView) As Boolean
        Dim retval As Boolean = True
        If GridTransaksi.RowCount > 0 Then
            If GridTransaksi.RowCount = 1 And GridTransaksi.Rows(0).IsNewRow Then
                retval = False
            End If
        Else
            retval = False
        End If
        Return retval


    End Function
    Private Sub GridTransaksi_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyUp
        Dim ic, ir As Integer
        If validasirowgrid(GridTransaksi) Then
            ic = GridTransaksi.CurrentCell.ColumnIndex
            ir = GridTransaksi.CurrentCell.RowIndex
        Else
            Exit Sub
        End If

        If e.KeyValue = 13 Then
            If GridTransaksi.CurrentCell.ColumnIndex = 1 Or GridTransaksi.CurrentCell.ColumnIndex = 2 Then
                If Not IsDBNull(GridTransaksi(0, ir).Value) Or Not IsDBNull(GridTransaksi.CurrentCell.Value) Then
                    If Not GridTransaksi(0, ir).Value = "" Or Not GridTransaksi(1, ir).Value = "" Then



                        If Not IsDBNull(GridTransaksi(3, ir).Value) Then
                            If GridTransaksi(3, ir).Value = 0 Then
                                GridTransaksi(3, ir).Value = 1
                                GridTransaksi(5, ir).Value = GridTransaksi(3, ir).Value * GridTransaksi(2, ir).Value
                                sumtotal()
                                sumhutang()
                                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                                GridTransaksi.CurrentCell.Selected = True
                            ElseIf GridTransaksi(3, ir).Value > 0 Then

                                GridTransaksi.CurrentCell = GridTransaksi(3, ir)
                                GridTransaksi.CurrentCell.Selected = True
                            End If
                        Else
                            GridTransaksi.CurrentCell = GridTransaksi(3, ir)
                            GridTransaksi.CurrentCell.Selected = True
                        End If

                    Else
                        GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                        GridTransaksi.CurrentCell.Selected = True
                    End If
                Else
                    GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                    GridTransaksi.CurrentCell.Selected = True
                End If

                'Dim eKey As New KeyEventArgs(Keys.Return)
                'GridTransaksi_KeyUp(sender, eKey)
            ElseIf GridTransaksi.CurrentCell.ColumnIndex = 4 Or GridTransaksi.CurrentCell.ColumnIndex = 5 Then
                GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 1)
                GridTransaksi.CurrentCell.Selected = True
            End If
        End If
    End Sub

    Private Sub GridTransaksi_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridTransaksi.KeyDown
        If e.KeyValue = Keys.Delete Then
            Try
                Dim dgr As New DataGridViewRow
                dgr = GridTransaksi.CurrentRow
                GridTransaksi.Rows.Remove(dgr)
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub CariitemToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CariitemToolStripMenuItem.Click
        Dim DialogSearchItem As New DialogSearchItem
        DialogSearchItem.ShowDialog()
        Dim dgr As New DataGridViewRow
        dgr = DialogSearchItem.ReturnValue
        If dgr Is Nothing Then
            Exit Sub
        End If
        Dim str As String
        str = IIf(IsDBNull(dgr.Cells(0).Value), "", dgr.Cells(0).Value)
        If str = String.Empty Then
            Exit Sub
        Else
            GridTransaksi.Rows.Add()
            GridTransaksi(0, GridTransaksi.RowCount - 2).Value = str
            'GridTransaksi(1, GridTransaksi.RowCount - 2).Value = IIf(IsDBNull(dgr.Cells(1).Value), "", dgr.Cells(1).Value)
            'GridTransaksi(3, GridTransaksi.RowCount - 2).Value = 1
            GridTransaksi.Focus()
            GridTransaksi.CurrentCell = GridTransaksi(0, GridTransaksi.RowCount - 2)
            GridTransaksi(0, GridTransaksi.RowCount - 2).Selected = True
            GridTransaksi.BeginEdit(True)
            GridTransaksi.EndEdit(True)
        End If
    End Sub

    Private Sub sumtotal()
        'count item
        If GridTransaksi.Rows.Count > 0 Then
            Dim i As Integer = 0
            Dim d As Double = 0
            i = GridTransaksi.Rows.GetLastRow(DataGridViewElementStates.None)
            If GridTransaksi.Rows(i).IsNewRow Then
                txtTotalItem.Text = GridTransaksi.Rows.Count - 1
                d = 0
                For ir As Integer = 0 To GridTransaksi.Rows.Count - 1
                    d += GridTransaksi("total", ir).Value
                Next
                txtTotal.Text = d.ToString
            Else
                txtTotalItem.Text = GridTransaksi.Rows.Count
                d = 0
                For ir As Integer = 0 To GridTransaksi.Rows.Count
                    d += GridTransaksi("total", ir).Value
                Next
                txtTotal.Text = d.ToString
            End If
        ElseIf GridTransaksi.Rows.Count = 0 Then
            txtTotalItem.Text = "0"
            txtTotal.Text = "0"
        End If

    End Sub

    Private Sub clearHeader()
        For Each ctl As Control In PanHeader.Controls
            If TypeOf ctl Is TextBox Then
                ctl.Text = ""
            End If
        Next
        txtTotal.Text = "0"
        txtTotalItem.Text = "0"
        lblviabank.Text = ""
        lblnotrxbank.Text = ""

        cbxOrder.Checked = False
    End Sub
    Private Sub getnotrx()
        txtnopo.Text = ""
        Dim dt As New DataTable
        If Utils.GetFunction("getidtrx", New Object() {"bl"}, dt) Then
            If Utils.RowExist(dt) Then
                txtnopo.Text = dt(0)(0)
            End If
        End If
    End Sub
    Private Sub setCombo()
        Dim dt As New DataTable
        If Utils.executeSP("proc_searchall", New Object() {"*", "rssupplier", "nama", ""}, dt) Then
            If Utils.RowExist(dt) Then
                cmbnamasupp.DataSource = dt
                cmbnamasupp.DisplayMember = "nama"
                cmbnamasupp.ValueMember = "idsupplier"
            End If
        End If
    End Sub
    Private Sub TrxPembelianPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clearHeader()
        GridTransaksi.Rows.Clear()
        'setCombo()
        getnotrx()
        setCombo()
        dtpTrans.Value = Now
        getGudang()
    End Sub

    Private Sub cmbnamasupp_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbnamasupp.SelectedValueChanged
        If Not cmbnamasupp.SelectedValue.ToString = "System.Data.DataRowView" Then


            If cmbnamasupp.DataSource IsNot Nothing Then

                Dim dt As New DataTable
                dt = cmbnamasupp.DataSource
                Dim r() As DataRow
                r = dt.Select("idsupplier=" & cmbnamasupp.SelectedValue)
                If r.Length > 0 Then
                    txtalamat.Text = IIf(IsDBNull(r(0).Item("alamat")), "", r(0).Item("alamat"))
                    txttelp.Text = IIf(IsDBNull(r(0).Item("telp")), "", r(0).Item("telp"))
                End If
            End If
        End If
    End Sub

    Private Sub cmbnamasupp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If cmbnamasupp.SelectedValue.ToString = "System.Data.DataRowView" Then
            Exit Sub
        End If
        If cmbnamasupp.DataSource IsNot Nothing Then

            Dim dt As New DataTable
            dt = cmbnamasupp.DataSource
            Dim r() As DataRow
            r = dt.Select("idsupplier=" & cmbnamasupp.SelectedValue)
            If r.Length > 0 Then
                txtalamat.Text = IIf(IsDBNull(r(0).Item("alamat")), "", r(0).Item("alamat"))
                txttelp.Text = IIf(IsDBNull(r(0).Item("telp")), "", r(0).Item("telp"))
            End If
        End If
    End Sub
   
    Private Sub AktifgridToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AktifgridToolStripMenuItem.Click
        GridTransaksi.Focus()
    End Sub

    Private Sub FocusupplierToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FocusupplierToolStripMenuItem.Click
        cmbnamasupp.Focus()
    End Sub

    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
        clearHeader()
        GridTransaksi.Rows.Clear()
        'setCombo()
        getnotrx()
        setCombo()
        dtpTrans.Value = Now
        getGudang()

        sumhutang()

    End Sub
    Private Function FindControlValue(ByVal ctlColect As ControlCollection, ByVal paramcari As String) As string    
        Dim retval As String = ""
        For Each ctl1 As Control In ctlColect
            If paramcari.Trim = ctl1.Tag Then
                If TypeOf ctl1 Is TextBox Then

                    If TypeOf ctl1 Is AMS.TextBox.CurrencyTextBox Then
                        Dim ctldim As New AMS.TextBox.CurrencyTextBox
                        ctldim = CType(ctl1, AMS.TextBox.CurrencyTextBox)
                        retval = ctldim.NumericText
                    ElseIf TypeOf ctl1 Is AMS.TextBox.NumericTextBox Then
                        Dim ctldim As New AMS.TextBox.NumericTextBox
                        ctldim = CType(ctl1, AMS.TextBox.NumericTextBox)
                        retval = ctldim.NumericText
                    Else
                        retval = ctl1.Text
                    End If

                ElseIf TypeOf ctl1 Is ComboBox Then
                    Dim ctldim As New ComboBox
                    ctldim = CType(ctl1, ComboBox)
                    If ctldim.DataSource IsNot Nothing Then
                        retval = ctldim.SelectedValue
                    Else
                        retval = ctldim.Text
                    End If
                ElseIf TypeOf ctl1 Is DateTimePicker Then
                    Dim ctldim As New DateTimePicker
                    ctldim = CType(ctl1, DateTimePicker)
                    retval = ctldim.Value.ToString("yyyy-MM-dd")
                ElseIf TypeOf ctl1 Is Label Then
                    If ctl1.Tag = "itemlocation" Then
                        Dim strGudang() As String
                        strGudang = lblGudang.Text.Split(":")
                        retval = strGudang(1).Trim
                    Else
                        retval = ctl1.Text
                    End If
                End If
            End If
        Next
        Return retval
    End Function
    Private Function setParamInsert() As String
        Dim strParam As String = ""
        Dim paramArr As New List(Of String)
        paramArr.Clear()
        Const str As String = "idtrx, tanggal, nonota, idsupplier, itemlocation, total, discper, discrp, subtotal, bayar, hutang, viabank, catatby, notrxbank"
        Dim strArr As String()
        strArr = str.Split(",")
        For Each strval As String In strArr
            If strval.Trim = "catatby" Then
                paramArr.Add(LocalSession.operatorname)
            Else
                strParam = FindControlValue(PanHeader.Controls, strval)
                If strParam = String.Empty Then
                    strParam = FindControlValue(PanBottom.Controls, strval)
                End If
                paramArr.Add(strParam)
            End If
        Next
        strParam = String.Join(",", paramArr.ToArray)
        Return strParam
    End Function

    Private Sub SimpanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        Dim retval As Boolean = True
        If cmbnamasupp.SelectedValue.ToString = "" Then
            retval = False
        ElseIf txtTotalItem.NumericText = 0 Then
            retval = False
        ElseIf txtNota.Text = String.Empty Then
            retval = False
        ElseIf lblGudang.Text = String.Empty Then
            retval = False
        End If
        If Not retval Then
            MsgBox("Maaf Data Entry Tidak Valid")
            Exit Sub
        End If

        getnotrx()
        

        Dim dt As New DataTable
        Dim str As String = ""
        If Utils.exec_SP("proc_beli", New Object() {txtnopo.Text, 1, 14, setParamInsert()}) Then
            'If Utils.RowExist(dt) Then
            If cbxOrder.Checked Then
                If Utils.exec_SP("realisasiOrder", New Object() {cmbOrder.SelectedValue}) Then

                End If
            End If
            str = "Execute Finish"
            'End If
            Dim strdata As String = ""
            strdata = Utils.GridValueToText(GridTransaksi)
            Dim params As String()

            If Not strdata = "" Then
                params = strdata.Split("|")
                For int As Integer = 0 To params.Length - 1
                    params(int) = params(int).Insert(0, txtnopo.Text & ",")
                Next
                strdata = String.Join("|", params)
                If strdata.EndsWith("|") Then
                    strdata = LSet(strdata, strdata.Length - 1)
                End If
                If Utils.exec_SP("proc_belid", New Object() {txtnopo.Text, txtTotalItem.NumericText, GridTransaksi.ColumnCount + 1, strdata}) Then
                    If Utils.exec_SP("proc_setstokexec", New Object() {txtnopo.Text}) Then

                    End If
                    MsgBox(str)
                    clearHeader()
                    GridTransaksi.Rows.Clear()
                    setCombo()
                    getnotrx()
                    setCombo()

                End If
            End If
        End If

    End Sub

    Private Sub TrxPembelianPage_VisibleChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
        clearHeader()
        GridTransaksi.Rows.Clear()
        'setCombo()
        getnotrx()
        setCombo()
        dtpTrans.Value = Now
        getGudang()
    End Sub

    Private Sub GridTransaksi_RowsRemoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles GridTransaksi.RowsRemoved
        sumtotal()
        sumhutang()
    End Sub

    Private Sub cbxOrder_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxOrder.CheckedChanged
        Dim dt As New DataTable
        Dim retmsg As String = ""
        If cbxOrder.Checked Then
            If Utils.executeSP("lookup_OrderBeli", New Object() {"lookup", "lookup_orderbeli", cmbnamasupp.SelectedValue.ToString}, dt) Then
                If Utils.RowExist(dt) Then
                    retmsg = dt(0)(0)
                End If

                If retmsg = "No Data Found" Then
                    MsgBox(retmsg)
                    cmbOrder.Enabled = False
                    cmbOrder.DataSource = Nothing
                    cbxOrder.Checked = False
                Else
                    cmbOrder.Enabled = True
                    cmbOrder.DataSource = dt
                    cmbOrder.ValueMember = "idtrx"
                    cmbOrder.DisplayMember = "idtrx"
                End If
            End If
        Else
            cmbOrder.Enabled = False
            cmbOrder.DataSource = Nothing
        End If
    End Sub

    Private Sub cmbOrder_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbOrder.SelectedValueChanged
        If cmbOrder.DataSource IsNot Nothing Then


            If Not cmbOrder.SelectedValue.ToString = "System.Data.DataRowView" Then
                Dim dt As New DataTable
                Dim param As Object()
                If Not txtnopo.Text = String.Empty Then
                    If cbxOrder.Checked Then
                        param = New Object() {"getbarang", cmbOrder.SelectedValue.ToString, cmbnamasupp.SelectedValue.ToString}
                        If Utils.executeSP("lookup_orderbeli", param, dt) Then
                            If Utils.RowExist(dt) Then
                                Dim paramarr() As Object
                                GridTransaksi.Rows.Clear()
                                If dt.Columns.Count > 1 Then

                                    For Each r As DataRow In dt.Rows
                                        Dim dgr As New DataGridViewRow
                                        paramarr = New Object() {r("iditem").ToString, r("namaitem").ToString, r("hargabeli").ToString, r("qty").ToString, r("satuanbeli").ToString, r("subtotal").ToString}
                                        GridTransaksi.Rows.Insert(GridTransaksi.Rows.Count - 1, paramarr)
                                        sumtotal()
                                        sumhutang()
                                    Next
                                End If
                            End If
                        End If
                    End If
                    GridTransaksi.Focus()
                Else
                    MsgBox("Data Belum Lengkap, Pilih Baru terlebih dahulu", , "Perhatian")
                End If
            End If
        End If
    End Sub
    Private Sub sumhutang()
        Dim total As Double
        Dim subtotal As Double
        'Dim totalg As Double
        Try
            total = IIf(txtTotal.NumericText = String.Empty, 0, txtTotal.NumericText)
            subtotal = total - CDbl(txtDiscRp.NumericText)
            txtsubtotal.Text = CStr(subtotal)

            If subtotal > CDbl(txtBayar.NumericText) Or subtotal = CDbl(txtBayar.NumericText) Then
                txtHutang.Text = subtotal - CDbl(txtBayar.NumericText)
            Else
                txtHutang.Text = 0
            End If
        Catch ex As Exception

        End Try


    End Sub
    Private Sub txtDiscRp_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDiscRp.KeyUp
        If txtDiscRp.NumericText = String.Empty Then
            txtDiscRp.Text = "0"

        ElseIf txtnopo.Text = String.Empty Then
            txtDiscRp.Text = 0
        ElseIf txtTotal.NumericText = String.Empty Or txtTotal.NumericText = "0" Then
            txtDiscRp.Text = "0"
        End If
        'If cbxDiscPersen.Checked Then
        '    Dim rp As Double
        '    rp = CDbl(GridControl1.txtTotal.NumericText) * (CDbl(txtDiscPersen.Text) / 100)
        '    txtDiscRp.Text = CStr(rp)
        'End If
        sumhutang()
    End Sub

    Private Sub txtDiscPersen_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDiscPersen.TextChanged
        If cbxDiscPersen.Checked Then
            Dim rp As Double
            rp = 0
            Try
                rp = CDbl(txtTotal.NumericText) * (CDbl(txtDiscPersen.Text) / 100)
            Catch ex As Exception

            End Try

            txtDiscRp.Text = CStr(rp)
            sumhutang()
        End If
    End Sub

    Private Sub cbxDiscPersen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxDiscPersen.CheckedChanged
        If cbxDiscPersen.Checked Then
            txtDiscPersen.Enabled = True
            txtDiscPersen_TextChanged(sender, e)
        Else

            txtDiscPersen.Text = 0
            Dim rp As Double
            rp = 0
            Try
                rp = CDbl(txtTotal.NumericText) * (CDbl(txtDiscPersen.Text) / 100)
            Catch ex As Exception

            End Try

            txtDiscRp.Text = CStr(rp)
            sumhutang()
            txtDiscPersen.Enabled = False
        End If

    End Sub

    Private Sub BayarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BayarToolStripMenuItem.Click
        If CDbl(txtsubtotal.NumericText) <= 0 Then
            Exit Sub
        End If
        Dim retval As Boolean = True
        If cmbnamasupp.SelectedValue.ToString = "" Then
            retval = False
        ElseIf txtTotalItem.NumericText = 0 Then
            retval = False
        ElseIf txtNota.Text = String.Empty Then
            retval = False
        ElseIf lblGudang.Text = String.Empty Then
            retval = False
        End If
        If Not retval Then
            MsgBox("Maaf Data Entry Tidak Valid")
            Exit Sub
        End If
        Dim DialogBayarAll As New DialogBayarAll
        DialogBayarAll.txtTotal.Text = txtsubtotal.NumericText
        DialogBayarAll.parentme = Me.Name
        DialogBayarAll.ShowDialog()
        If DialogBayarAll.returnbayar Then
            txtBayar.Text = DialogBayarAll.txtBayar.NumericText
            lblnotrxbank.Text = DialogBayarAll.ComBank.Text
            lblnotrxbank.Text = DialogBayarAll.txtnumbank.Text
            sumhutang()
        End If
    End Sub

  
  
End Class
